
capture log close

set matsize 10000

*global path "C:\Users\Jeff\Dropbox\MedicalInnovationProjects\CivilWarProject"


log using "LogFiles/WarsAndProsthesisPatentTrendsSynth.log", replace



*** Assemble data
use "Data/control_classes_CW.dta", clear
append using "Data/control_classes_WWI.dta"
merge 1:1 patnum using "Data/all_patents_basicinfo.dta"
keep if _merge == 3
drop _merge
merge 1:1 patnum using "Data/control_classes_KWSearch.dta"
keep if _merge == 3
drop _merge

tab iyear if nclassgoogle1 == 623

*** Generate a numeric variable for the individual war episodes
gen warepisode = .
replace warepisode = 1 if iyear >= 1840 & iyear <= 1890
replace warepisode = 2 if iyear >= 1890 & iyear <= 1940

rename iyear year

merge m:1 main_uspto using "Data/with_header_subclass_formerge.dta"
tab _merge
drop if _merge == 1
drop if _merge == 2
drop _merge

*gen nclassgoogle2digit1 = substr(nclassgoogle2,1,1) 
*egen main_usptonum = group(nclassgoogle1 nclassgoogle2digit1)
gen patentcount = 1
gen prosthetics = nclassgoogle1 == 623  



gen user = (comfort + appearance + appliances)/3  
gen production = (adjustability + simplicity + cost)/3 
gen anyproduction = max(adjustability, simplicity, cost, materials) 

*** Generates table D.4
*** Summary statistics and correlations across traits
sum production user cost simplicity adjustability appliances appearance comfort durability materials 
cor cost simplicity adjustability appliances appearance comfort durability materials
sum production user cost simplicity adjustability appliances appearance comfort durability materials if nclassgoogle1 == 623
cor cost simplicity adjustability appliances appearance comfort durability materials if nclassgoogle1 == 623

save "Data/CivilWarIntemediateData.dta", replace

preserve 

use "Data/CivilWarIntemediateData.dta", clear

collapse (sum) patentcount (mean) adjustability simplicity cost appearance comfort materials appliances production durability user, by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in patentcount adjustability simplicity cost appearance comfort materials appliances production durability user {
	replace `var' = 0 if `var' == .
}


foreach var in adjustability simplicity cost appearance comfort materials appliances production durability user {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

gen prosthetics = nclassgoogle1 == 623

gen warepisode = .
replace warepisode = 1 if year >= 1840 & year <= 1890
replace warepisode = 2 if year >= 1890 & year <= 1940

gen period = .
replace period = 0 if year >= 1855 & year <= 1861
replace period = 1 if year >= 1862 & year <= 1866

replace period = 0 if year >= 1910 & year <= 1915
replace period = 1 if year >= 1916 & year <= 1922

drop if period == .

*** Code to produce summary diff-in-diff bar charts
collapse fouryearadjustability fouryearsimplicity fouryearcost fouryearappearance fouryearcomfort fouryearmaterials fouryearappliances fouryearproduction fouryeardurability fouryearuser, by(warepisode prosthetics period)

foreach var in adjustability simplicity cost appearance comfort materials appliances production durability user {
	label var fouryear`var' "`var'"
}

capture eststo clear
eststo: estpost tabstat fouryearproduction fouryearuser fouryearcost fouryearsimplicity fouryearadjustability fouryearappliances fouryearcomfort fouryearappearance fouryeardurability fouryearmaterials ///
	if warepisode == 1 & period == 0 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat fouryearproduction fouryearuser fouryearcost fouryearsimplicity fouryearadjustability fouryearappliances fouryearcomfort fouryearappearance fouryeardurability fouryearmaterials ///
	if warepisode == 1 & period == 0 & prosthetics == 0, columns(statistics) statistics(mean)
** Appendix Table C.1: Baseline Summary Statistics for Prosthetic Devices, All Control Classes, and Re-Weighted Synthetic Control Classes
**	Baseline Summary Statistics for Prosthetic Devices, All Control Classes, and Re-Weighted Synthetic Control Classes
esttab using "Tables/SummaryStatsByWarAndTreatmentMovingAve.tex", replace main(mean) aux(sd) label nomtitle nolegend nonotes compress

** Appendix Table C.1 (Appending WW): Baseline Summary Statistics for Prosthetic Devices, All Control Classes, and Re-Weighted Synthetic Control Classes
capture eststo clear
eststo: estpost tabstat fouryearproduction fouryearuser fouryearcost fouryearsimplicity fouryearadjustability fouryearappliances fouryearcomfort fouryearappearance fouryeardurability fouryearmaterials ///
	if warepisode == 2 & period == 0 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat fouryearproduction fouryearuser fouryearcost fouryearsimplicity fouryearadjustability fouryearappliances fouryearcomfort fouryearappearance fouryeardurability fouryearmaterials  ///
	if warepisode == 2 & period == 0 & prosthetics == 0, columns(statistics) statistics(mean)

esttab using "Tables/SummaryStatsByWarAndTreatmentMovingAve.tex", append main(mean) aux(sd) label nomtitle nolegend nonotes compress
	
restore


// Creating all synthetic control estimates for all traits for Civil War. To be used in Figures 2, 3, A.2, D.7, D.8, D.9, D.10, D.11, Table 5
foreach trait in adjustability simplicity cost appearance comfort materials production durability user {

preserve 

drop if year < 1850 | year > 1875

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in `trait' {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in `trait' {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}


drop if year < 1851

egen basepatent`trait'B = total(`trait') if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year >= 1855
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

synth fouryear`trait' fouryear`trait'(1861) fouryear`trait'(1860) fouryear`trait'(1859) fouryear`trait'(1858) fouryear`trait'(1857) fouryear`trait'(1856) fouryear`trait'(1855), trunit(623) trperiod(1862) keep("Data/SyntheticControlCIV`trait'.dta") replace

*** Loop over categories to construct synth output for placebo distributions
levelsof nclassgoogle1, local(classlist)

foreach class of local classlist { 
synth fouryear`trait' fouryear`trait'(1861) fouryear`trait'(1860) fouryear`trait'(1859) fouryear`trait'(1858) fouryear`trait'(1857) fouryear`trait'(1856) fouryear`trait'(1855), trunit(`class') trperiod(1862) keep("Data/synthdatasets/SyntheticControlCIV`trait'`class'.dta") replace
}

use "Data/SyntheticControlCIV`trait'.dta", clear

rename _time year
rename _Y_synthetic fouryear`trait'control
rename _Y_treated  fouryear`trait'treatment
tsset year 

// Creating synthetic control estimate figures for all traits for Civil War. To be used in Figures 2, 3, A.2, D.7, D.8, D.9, D.10, and D.11., Table 5
twoway (tsline fouryear`trait'treatment if year >= 1855 & year <= 1870, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryear`trait'control if year >= 1855 & year <= 1870, lpattern(dash) lwidth(medthick)), ytitle(Mean Emphasis on Trait) title(Mean Emphasis on Trait by Year: `trait', size(medlarge) color(black)) xlabel(1855(5)1870) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) ylabel(0(.15).6) scale(0.9) name(`trait'ShareCIVSynth, replace) 
graph export "Figures/Synth`trait'CIV.pdf", replace name(`trait'ShareCIVSynth)

gen warepisode = .
replace warepisode = 1 if year >= 1840 & year <= 1890
replace warepisode = 2 if year >= 1890 & year <= 1940

gen period = 0 
replace period = 1 if year >= 1862 & year <= 1866
replace period = 1 if year >= 1916 & year <= 1922
replace period = 2 if year >= 1867 & year <= 1880
replace period = 2 if year >= 1923 & year <= 1940

*** Code to produce summary diff-in-diff bar charts
collapse fouryear`trait'treatment fouryear`trait'control, by(warepisode period)
drop if warepisode == .

reshape long fouryear`trait', i(period) j(treatmentstatus) string
gen prosthetics = 0
replace prosthetics = 1 if treatmentstatus == "treatment"
drop treatmentstatus 

label define prostheticslabels 0 "Other" 1 "Prosthetics", replace
label values prosthetics prostheticslabels, 
label define periodlabels 0 "Pre War Patents" 1 "Wartime Patents" 2 "Post War", replace
label values period periodlabels, 

rename fouryear`trait' `trait'
graph hbar `trait' if period != 2, ytitle("Mean emphasis on `trait'", size(small)) over(period, label(labsize(small))) over(prosthetics, label(labsize(small))) graphregion(color(white)) title(Civil War: `trait', size(medlarge) color(black)) ylabel(0(.15).6) name(`trait'ShareCIVSynthBar, replace)
graph export "Figures/Synth`trait'CIVBar.pdf", replace name(`trait'ShareCIVSynthBar)
sum `trait' if period == 0 & prosthetics == 1
sum `trait' if period == 0 & prosthetics == 0
sum `trait' if period == 1 & prosthetics == 1
sum `trait' if period == 1 & prosthetics == 0

save "Data/synthdatasets/SynthDataPrePostCIV`trait'.dta", replace 

restore

}



foreach trait in adjustability simplicity cost appearance comfort materials production durability user {
use "Data/CivilWarIntemediateData.dta", clear

drop if year < 1850 | year > 1875

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}


drop if year < 1851

egen basepatent`trait'B = total(`trait') if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year >= 1855
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

levelsof nclassgoogle1, local(classlist)

	foreach class of local classlist { 
	preserve
	use "Data/synthdatasets/SyntheticControlCIV`trait'`class'.dta", clear

	rename _time year
	rename _Y_synthetic fouryear`trait'control
	rename _Y_treated  fouryear`trait'treatment
	tsset year 

	gen period = .
	replace period = 0 if year >= 1855 & year <= 1861
	replace period = 1 if year >= 1862 & year <= 1863

	egen treatmentperiod0B = mean(fouryear`trait'treatment) if period == 0
	egen treatmentperiod0 = mean(treatmentperiod0B) 
	egen treatmentperiod1B = mean(fouryear`trait'treatment) if period == 1
	egen treatmentperiod1 = mean(treatmentperiod1B) 

	egen controlperiod0B = mean(fouryear`trait'control) if period == 0
	egen controlperiod0 = mean(controlperiod0B) 
	egen controlperiod1B = mean(fouryear`trait'control) if period == 1
	egen controlperiod1 = mean(controlperiod1B) 
	
	gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)

	keep if year == 1860
	keep diffindiff 
	gen true = 0

	gen class = `class'

	save "Data/synthresults/SynthResultLeadingEdgeCivWar`trait'`class'.dta", replace
	restore 

	}

	preserve
	levelsof nclassgoogle1, local(classlist)
	use "Data/synthresults/SynthResultLeadingEdgeCivWar`trait'16.dta", clear 
	foreach class of local classlist { 
	append using "Data/synthresults/SynthResultLeadingEdgeCivWar`trait'`class'.dta"
	}
	duplicates drop _all, force
	
	save "Data/synthresults/SynthResultLeadingEdgeCivWar`trait'allclasses.dta", replace
	restore

}





foreach trait in adjustability simplicity cost appearance comfort materials production durability user {
use "Data/CivilWarIntemediateData.dta", clear


drop if year < 1850 | year > 1875

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}


drop if year < 1851

egen basepatent`trait'B = total(`trait') if year >= 1855 & year <= 1861 , by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year >= 1855
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

levelsof nclassgoogle1, local(classlist)

	foreach class of local classlist { 
	preserve
	use "Data/synthdatasets/SyntheticControlCIV`trait'`class'.dta", clear

	rename _time year
	rename _Y_synthetic fouryear`trait'control
	rename _Y_treated  fouryear`trait'treatment
	tsset year 

	gen period = .
	replace period = 0 if year >= 1855 & year <= 1861
	replace period = 1 if year >= 1862 & year <= 1866

	egen treatmentperiod0B = mean(fouryear`trait'treatment) if period == 0
	egen treatmentperiod0 = mean(treatmentperiod0B) 
	egen treatmentperiod1B = mean(fouryear`trait'treatment) if period == 1
	egen treatmentperiod1 = mean(treatmentperiod1B) 

	egen controlperiod0B = mean(fouryear`trait'control) if period == 0
	egen controlperiod0 = mean(controlperiod0B) 
	egen controlperiod1B = mean(fouryear`trait'control) if period == 1
	egen controlperiod1 = mean(controlperiod1B) 
	
	gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)

	keep if year == 1860
	keep diffindiff 
	gen true = 0

	gen class = `class'

	save "Data/synthresults/SynthResultFullBoomCivWar`trait'`class'.dta", replace
	restore 

	}

	preserve
	levelsof nclassgoogle1, local(classlist)
	use "Data/synthresults/SynthResultFullBoomCivWar`trait'16.dta", clear 
	foreach class of local classlist { 
	append using "Data/synthresults/SynthResultFullBoomCivWar`trait'`class'.dta"
	}
	duplicates drop _all, force
	
	save "Data/synthresults/SynthResultFullBoomCivWar`trait'allclasses.dta", replace
	restore

}









// Creating all synthetic control estimates for all traits for Civil War. To be used in Figures 2, 3, A.2, D.7, D.8, D.9, D.10, D.11, Table 5
*** WWI Loop
use "Data/CivilWarIntemediateData.dta", clear

foreach trait in adjustability simplicity cost appearance comfort materials appliances production durability user {

preserve 

drop if year < 1905 | year > 1930

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in `trait' {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in `trait' {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}


drop if year < 1905

egen basepatent`trait'B = total(`trait') if year >= 1910 & year <= 1915, by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year > 1910
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1910

synth fouryear`trait' fouryear`trait'(1915) fouryear`trait'(1914) fouryear`trait'(1913) fouryear`trait'(1912) fouryear`trait'(1911) fouryear`trait'(1910), trunit(623) trperiod(1916) keep("Data/SyntheticControlWWI`trait'.dta") replace

*** Loop over categories to construct synth output for placebo distributions
levelsof nclassgoogle1, local(classlist)

foreach class of local classlist { 
synth fouryear`trait' fouryear`trait'(1915) fouryear`trait'(1914) fouryear`trait'(1913) fouryear`trait'(1912) fouryear`trait'(1911) fouryear`trait'(1910), trunit(`class') trperiod(1916) keep("Data/synthdatasets/SyntheticControlWWI`trait'`class'.dta") replace
}


use "Data/SyntheticControlWWI`trait'.dta", clear

rename _time year
rename _Y_synthetic fouryear`trait'control
rename _Y_treated  fouryear`trait'treatment
tsset year 

// Appendix Figure D.8 
twoway (tsline fouryear`trait'treatment if year >= 1910 & year <= 1925, xline(1916, lcolor(black) lpattern(dash)) xline(1923, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline fouryear`trait'control if year >= 1910 & year <= 1925, lpattern(dash) lwidth(medthick)), ytitle(Mean Emphasis on Trait) title(Mean Emphasis on Trait by Year: `trait', size(medlarge) color(black)) xlabel(1910(5)1925) legend(label(1 "Prosthetics") label(2 "Synthetic Control")) graphregion(fcolor(white)) ylabel(0(.15).6) scale(0.9) name(`trait'ShareWWISynth, replace) 
graph export "Figures/Synth`trait'WWI.pdf", replace name(`trait'ShareWWISynth)

gen warepisode = .
replace warepisode = 1 if year >= 1840 & year <= 1890
replace warepisode = 2 if year >= 1890 & year <= 1940

gen period = 0 
replace period = 1 if year >= 1862 & year <= 1866
replace period = 1 if year >= 1916 & year <= 1922
replace period = 2 if year >= 1867 & year <= 1880
replace period = 2 if year >= 1923 & year <= 1940

*** Code to produce summary diff-in-diff bar charts
collapse fouryear`trait'treatment fouryear`trait'control, by(warepisode period)
drop if warepisode == .

reshape long fouryear`trait', i(period) j(treatmentstatus) string
gen prosthetics = 0
replace prosthetics = 1 if treatmentstatus == "treatment"
drop treatmentstatus 

label define prostheticslabels 0 "Other" 1 "Prosthetics", replace
label values prosthetics prostheticslabels, 
label define periodlabels 0 "Pre War Patents" 1 "Wartime Patents" 2 "Post War", replace
label values period periodlabels, 

rename fouryear`trait' `trait'

graph hbar `trait' if period != 2, ytitle("Mean emphasis on `trait'", size(small)) over(period, label(labsize(small))) over(prosthetics, label(labsize(small))) graphregion(color(white)) title(World War I: `trait', size(medlarge) color(black)) ylabel(0(.15).6) name(`trait'ShareWWISynthBar, replace)
graph export "Figures/Synth`trait'WWIBar.pdf", replace name(`trait'ShareWWISynthBar)
sum `trait' if period == 0 & prosthetics == 1
sum `trait' if period == 0 & prosthetics == 0
sum `trait' if period == 1 & prosthetics == 1
sum `trait' if period == 1 & prosthetics == 0

save "Data/synthdatasets/SynthDataPrePostWWI`trait'.dta", replace 


restore


}
   

   
foreach trait in adjustability simplicity cost appearance comfort materials appliances production durability user {
use "Data/CivilWarIntemediateData.dta", clear

drop if year < 1905 | year > 1930

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}


drop if year < 1905

egen basepatent`trait'B = total(`trait') if year >= 1910 & year <= 1915 , by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year >= 1910
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

levelsof nclassgoogle1, local(classlist)

	foreach class of local classlist { 
	preserve
	use "Data/synthdatasets/SyntheticControlWWI`trait'`class'.dta", clear

	rename _time year
	rename _Y_synthetic fouryear`trait'control
	rename _Y_treated  fouryear`trait'treatment
	tsset year 

	gen period = .
	replace period = 0 if year >= 1910 & year <= 1915
	replace period = 1 if year >= 1916 & year <= 1918

	egen treatmentperiod0B = mean(fouryear`trait'treatment) if period == 0
	egen treatmentperiod0 = mean(treatmentperiod0B) 
	egen treatmentperiod1B = mean(fouryear`trait'treatment) if period == 1
	egen treatmentperiod1 = mean(treatmentperiod1B) 

	egen controlperiod0B = mean(fouryear`trait'control) if period == 0
	egen controlperiod0 = mean(controlperiod0B) 
	egen controlperiod1B = mean(fouryear`trait'control) if period == 1
	egen controlperiod1 = mean(controlperiod1B) 
	
	gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)

	keep if year == 1910
	keep diffindiff 
	gen true = 0

	gen class = `class'

	save "Data/synthresults/SynthResultLeadingEdgeWWI`trait'`class'.dta", replace
	restore 

	}

	preserve
	levelsof nclassgoogle1, local(classlist)
	use "Data/synthresults/SynthResultLeadingEdgeWWI`trait'16.dta", clear 
	foreach class of local classlist { 
	append using "Data/synthresults/SynthResultLeadingEdgeWWI`trait'`class'.dta"
	}
	duplicates drop _all, force
	
	save "Data/synthresults/SynthResultLeadingEdgeWWI`trait'allclasses.dta", replace
	restore

}





foreach trait in adjustability simplicity cost appearance comfort materials appliances production durability user {
use "Data/CivilWarIntemediateData.dta", clear

drop if year < 1905 | year > 1930

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}


drop if year < 1905

egen basepatent`trait'B = total(`trait') if year >= 1910 & year <= 1915 , by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year >= 1910
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

levelsof nclassgoogle1, local(classlist)

	foreach class of local classlist { 
	preserve
	use "Data/synthdatasets/SyntheticControlWWI`trait'`class'.dta", clear

	rename _time year
	rename _Y_synthetic fouryear`trait'control
	rename _Y_treated  fouryear`trait'treatment
	tsset year 

	gen period = .
	replace period = 0 if year >= 1910 & year <= 1915
	replace period = 1 if year >= 1916 & year <= 1922

	egen treatmentperiod0B = mean(fouryear`trait'treatment) if period == 0
	egen treatmentperiod0 = mean(treatmentperiod0B) 
	egen treatmentperiod1B = mean(fouryear`trait'treatment) if period == 1
	egen treatmentperiod1 = mean(treatmentperiod1B) 

	egen controlperiod0B = mean(fouryear`trait'control) if period == 0
	egen controlperiod0 = mean(controlperiod0B) 
	egen controlperiod1B = mean(fouryear`trait'control) if period == 1
	egen controlperiod1 = mean(controlperiod1B) 

	gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)

	keep if year == 1910
	keep diffindiff 
	gen true = 0

	gen class = `class'

	save "Data/synthresults/SynthResultFullBoomWWI`trait'`class'.dta", replace
	restore 

	}

	preserve
	levelsof nclassgoogle1, local(classlist)
	use "Data/synthresults/SynthResultFullBoomWWI`trait'16.dta", clear 
	foreach class of local classlist { 
	append using "Data/synthresults/SynthResultFullBoomWWI`trait'`class'.dta"
	}
	duplicates drop _all, force
	
	save "Data/synthresults/SynthResultFullBoomWWI`trait'allclasses.dta", replace
	restore

}


   
   
   
graph combine costShareCIVSynth simplicityShareCIVSynth comfortShareCIVSynth appearanceShareCIVSynth , cols(2) imargin(small) scale(.9) graphregion(color(white)) name(FourPanelTraitsShareCIVSynth, replace) ///
	title("Traits of Mechanical Patent Classes: Civil War Era", size(medlarge))
graph export "Figures/FourPanelTraitsShareCIVSynth.pdf", name(FourPanelTraitsShareCIVSynth) replace


graph combine costShareWWISynth simplicityShareWWISynth comfortShareWWISynth appearanceShareWWISynth, cols(2) imargin(small) scale(.9) graphregion(color(white)) name(FourPanelTraitsShareWWISynth, replace) ///
	title("Traits of Mechanical Patent Classes: World War I Era", size(medlarge))
graph export "Figures/FourPanelTraitsShareWWISynth.pdf", replace name(FourPanelTraitsShareWWISynth)



graph combine costShareCIVSynth simplicityShareCIVSynth adjustabilityShareCIVSynth materialsShareCIVSynth, cols(2) imargin(small) scale(.9) graphregion(color(white)) name(FourPanelProdShareCIVSynth, replace) ///
	title("Production Traits of Mechanical Patents: Civil War Era", size(medlarge))
graph export "Figures/FourPanelProdShareCIVSynth.pdf", name(FourPanelProdShareCIVSynth) replace


graph combine costShareWWISynth simplicityShareWWISynth adjustabilityShareWWISynth materialsShareWWISynth, cols(2) imargin(small) scale(.9) graphregion(color(white)) name(FourPanelProdShareWWISynth, replace) ///
	title("Production Traits of Mechanical Patents: World War I Era", size(medlarge))
graph export "Figures/FourPanelProdShareWWISynth.pdf", replace name(FourPanelProdShareWWISynth)


graph combine comfortShareCIVSynth comfortShareWWISynth appearanceShareCIVSynth appearanceShareWWISynth, cols(2) imargin(small) scale(.9) graphregion(color(white)) name(FourPaneluserShareWWISynth, replace) ///
	title("User Traits of Mechanical Patents", size(medlarge))
graph export "Figures/FourPanelUserShareWWICIVSynth.pdf", replace name(FourPaneluserShareWWISynth)



graph combine costShareCIVSynthBar simplicityShareCIVSynthBar adjustabilityShareCIVSynthBar materialsShareCIVSynthBar, cols(2) imargin(small) scale(.8) graphregion(color(white)) name(FourPanelProdShareCIVSynthBar, replace) ///
	title("Production Traits of Mechanical Patents: Civil War Era", size(medlarge))
graph export "Figures/FourPanelProdShareCIVSynthBar.pdf", name(FourPanelProdShareCIVSynthBar) replace


graph combine costShareWWISynthBar simplicityShareWWISynthBar adjustabilityShareWWISynthBar materialsShareWWISynthBar, cols(2) imargin(small) scale(.8) graphregion(color(white)) name(FourPanelProdShareWWISynthBar, replace) ///
	title("Production Traits of Mechanical Patents: World War I Era", size(medlarge))
graph export "Figures/FourPanelProdShareWWISynthBar.pdf", replace name(FourPanelProdShareWWISynthBar)


graph combine comfortShareCIVSynthBar comfortShareWWISynthBar appearanceShareCIVSynthBar appearanceShareWWISynthBar, cols(2) imargin(small) scale(.8) graphregion(color(white)) name(FourPaneluserShareWWIBar, replace) ///
	title("User Traits of Mechanical Patents", size(medlarge))
graph export "Figures/FourPanelUserShareWWICIVBar.pdf", replace name(FourPaneluserShareWWIBar)






foreach trait in adjustability simplicity cost appearance comfort materials appliances production durability user {
	use "Data/synthresults/SynthResultFullBoomWWI`trait'allclasses.dta", clear
	sum diffindiff if class == 623 
	display `r(mean)'
	hist diffindiff if class != 623, frequency title(WWI Full Boom `trait', size(medlarge)) color(white)  blcolor(red) width(.01) xline(`r(mean)', lcolor(black) lpattern(dash)) xlabel(-.3(.15).3) graphregion(color(white)) name(FullBoomWWI`trait'hist, replace)
	graph export "Figures/FullBoomWWI`trait'hist.pdf", replace name(FullBoomWWI`trait'hist)
	tab diffindiff
	gen estimate = diffindiff
	gen negativeestimateprostheticsB = 0 
	replace negativeestimateprostheticsB = 1 if diffindiff < 0 & class == 623
	egen negativeestimateprosthetics = max(negativeestimateprostheticsB) 
	replace diffindiff = -1*diffindiff if negativeestimateprosthetics == 1
	egen rank = rank(diffindiff)
	egen maxrank = max(rank)
	gen onesidedpvalue = (1 - rank/maxrank) if class == 623 
	gen twosidedpvalue = 2*(1 - rank/maxrank) if class == 623 
	replace twosidedpvalue = .99 if twosidedpvalue > .99
	save "Data/pvalueresults/PvalueDataSetWWI`trait'allclasses.dta", replace
	drop diffindiff
	keep if class == 623
	gen trait = "`trait'"
	save "Data/pvalueresults/PvalueDataSetWWI`trait'tableoutput.dta", replace
}

foreach trait in adjustability simplicity cost appearance comfort materials production durability user {
	use "Data/synthresults/SynthResultFullBoomCIVWAR`trait'allclasses.dta", clear
	sum diffindiff if class == 623 
	display `r(mean)'
	hist diffindiff if class != 623, frequency title(Civil War Full Boom `trait', size(medlarge)) color(white)  blcolor(red) width(.02) xline(`r(mean)', lcolor(black) lpattern(dash)) xlabel(-.6(.3).6) graphregion(color(white)) name(FullBoomCIV`trait'hist, replace)
	graph export "Figures/FullBoomCIV`trait'hist.pdf", replace name(FullBoomCIV`trait'hist)
	tab diffindiff
	gen estimate = diffindiff
	gen negativeestimateprostheticsB = 0 
	replace negativeestimateprostheticsB = 1 if diffindiff < 0 & class == 623
	egen negativeestimateprosthetics = max(negativeestimateprostheticsB) 
	replace diffindiff = -1*diffindiff if negativeestimateprosthetics == 1
	egen rank = rank(diffindiff)
	egen maxrank = max(rank)
	gen onesidedpvalue = (1 - rank/maxrank) if class == 623 
	gen twosidedpvalue = 2*(1 - rank/maxrank) if class == 623 
	replace twosidedpvalue = .99 if twosidedpvalue > .99
	save "Data/pvalueresults/PvalueDataSetCivWar`trait'allclasses.dta", replace
	drop diffindiff
	keep if class == 623
	gen trait = "`trait'"
	save "Data/pvalueresults/PvalueDataSetCivWar`trait'tableoutput.dta", replace
}

use "Data/pvalueresults/PvalueDataSetCIVWARcomforttableoutput.dta", clear
foreach trait in appearance adjustability simplicity cost materials durability production user {
	append using "Data/pvalueresults/PvalueDataSetCIVWAR`trait'tableoutput.dta"
}
foreach trait in comfort appearance appliances adjustability simplicity cost materials durability production user {
	append using "Data/pvalueresults/PvalueDataSetWWI`trait'tableoutput.dta"
}
// This .dta file has all the estimates of Table 5 Columns (2) and (4)
save "Data/pvalueresults/PvalueDataSetfulltableoutput.dta", replace


use "Data/synthdatasets/SynthDataPrePostCIVproduction.dta", clear 
gen trait = "production"
keep if period == 0
gen baselinemean = production
drop production
foreach trait in user cost simplicity adjustability comfort appearance durability materials {
	append using "Data/synthdatasets/SynthDataPrePostCIV`trait'.dta"
	replace trait = "`trait'" if trait == ""
	keep if period == 0
	replace baselinemean = `trait' if baselinemean == .
	drop `trait'
}
foreach trait in production user cost simplicity adjustability appliances comfort appearance durability materials {
	append using "Data/synthdatasets/SynthDataPrePostWWI`trait'.dta"
	replace trait = "`trait'" if trait == ""
	keep if period == 0
	replace baselinemean = `trait' if baselinemean == .
	drop `trait'
}
format baselinemean %9.3g 
save "Data/synthdatasets/SynthDataBaselineSumStats.dta", replace
drop if prosthetics == 1
// Table C.1 column 3:
save "Data/synthdatasets/SynthDataBaselineControlsSumStats.dta", replace


capture log close
